package com.foreverwin.jzymes.production.fqc.controller;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.service.ICustomFieldsAdviceService;
import com.foreverwin.jzymes.common.service.IItemAdviceService;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.annotation.ParamBind;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.event.AppEventPublisher;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.production.debur.domain.PackLog;
import com.foreverwin.jzymes.production.debur.service.IDeburPackAdviceService;
import com.foreverwin.jzymes.production.fqc.domain.FqcCheck;
import com.foreverwin.jzymes.production.fqc.domain.FqcCheckBox;
import com.foreverwin.jzymes.production.fqc.domain.FqcCheckNc;
import com.foreverwin.jzymes.production.fqc.service.IFqcAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/production/fqc")
public class FqcController {

    @Autowired
    IFqcAdviceService fqcAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    IDeburPackAdviceService deburPackAdviceService;
    @Autowired
    AppEventPublisher appEventPublisher;

    @RequestMapping("/fqcCheck")
    @ParamBind
    public AjaxResult fqcCheck(FqcCheck fqcCheck, ArrayList<FqcCheckBox> fqcCheckBoxs, ArrayList<FqcCheckNc> fqcCheckNcs ){
        try{
            fqcCheck.setSite( CommonMethods.getSite() );
            fqcAdviceService.fqcCheck( fqcCheck, fqcCheckBoxs, fqcCheckNcs );

            /**发布2H事件*/
            JSONObject eventData = new JSONObject();
            eventData.put( "SITE", fqcCheck.getSite() );
            eventData.put( "ITEM_BO", fqcCheck.getItemBo() );
            eventData.put( "RESRCE", fqcCheck.getResrce() );
            eventData.put( "QTY", fqcCheck.getLotQty() );
            AppEvent<JSONObject> appEvent = new AppEvent<>( this );
            appEvent.setName( "ITEM_CHECK_OUT_EVENT" );
            appEvent.setData( eventData );
            appEventPublisher.publishEvent( appEvent );

            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    @RequestMapping("/findItemPackLog")
    public AjaxResult findItemPackLog(@RequestParam String itemBo, @RequestParam String containerNumber ){
        List<PackLog> packLogList = deburPackAdviceService.findItemPackLog( itemBo, containerNumber );
        List<Map<String,Object>> rtnPackLogList = new ArrayList<>();
        for( PackLog packLog : packLogList ){
            Map<String,Object> rtnPackLogMap = new HashMap<>();

            Map<String,Object> itemMap = itemAdviceService.existItem( packLog.getItemBo() );
            rtnPackLogMap.put( "containerNumber", packLog.getContainerNumber() );
            rtnPackLogMap.put( "itemBo", packLog.getItemBo() );
            rtnPackLogMap.put( "item", itemMap.get( "ITEM" ) );
            rtnPackLogMap.put( "revision", itemMap.get( "REVISION" ) );
            rtnPackLogMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
            rtnPackLogMap.put( "container", packLog.getContainer() );
            rtnPackLogMap.put( "maxQty", packLog.getMaxQty() );
            rtnPackLogMap.put( "packQty", packLog.getPackQty() );
            rtnPackLogList.add( rtnPackLogMap );
        }
        return AjaxResult.returnSuccess().put( "value", rtnPackLogList );
    }
}
